/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

//import entidad.Project;
import entity.UserType;
import entity.Users;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.PersistenceUnit;
import javax.persistence.Query;

public class UsersFacade {

    private static final long serialVersionUID = 1L;

    private static final String PU = "WebPortalDB2";        

    EntityManagerFactory factory;

    public UsersFacade() {
    }

    public void create(Users entity) {
        System.out.println("Metodo create");
        factory = Persistence.createEntityManagerFactory(PU);
        EntityManager em = factory.createEntityManager();
        em.persist(entity);
        em.close();
        System.out.println("create1: Users successfuly inserted");
    }

    public void edit(Users entity) {
        factory = Persistence.createEntityManagerFactory(PU);
        EntityManager em = factory.createEntityManager();
        em.merge(entity);
        em.close();
        System.out.println("Users successfuly edited");
    }

    public void remove(Users entity) {
        factory = Persistence.createEntityManagerFactory(PU);
        EntityManager em = factory.createEntityManager();
        em.remove(em.merge(entity));
        em.close();
        System.out.println("Users successfuly removed");
    }

    public Users find(Object id) {
        Users res = null;
        factory = Persistence.createEntityManagerFactory(PU);
        EntityManager em = factory.createEntityManager();
        res = em.find(Users.class, id);
        return res;
    }

    public List<Users> findAll() {
        factory = Persistence.createEntityManagerFactory(PU);
        EntityManager em = factory.createEntityManager();
        javax.persistence.criteria.CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
        cq.select(cq.from(Users.class));
        return em.createQuery(cq).getResultList();
    }

    public void activate(Users users, Boolean bEstado) {
        factory = Persistence.createEntityManagerFactory(PU);
        EntityManager em = factory.createEntityManager();
        Query q = em.createNamedQuery("Users.UpdateActivation");
        q.setParameter("active", bEstado);
        q.setParameter("id", users.getId());
        // Query q = em.createQuery("UPDATE Agenda p SET p.habilitado = " + estado + " WHERE p.id = " +  id);    
         /*q.setParameter("id", id);
         q.setParameter("estado", estado);  */
        q.executeUpdate();
    }

    public boolean bFindByTypeNumberID(Users user, int op) {
        factory = Persistence.createEntityManagerFactory(PU);
        EntityManager em = factory.createEntityManager();
        Query q = null;

        if (op == 0) {
            //guardar
            q = em.createNamedQuery("Users.bFindByTypeNumberID");
        } else {
            //modificar
            q = em.createNamedQuery("Users.bFindByTypeNumberIDEdit");
            q.setParameter("id", user.getId());
        }//fin if

        q.setParameter("iDtype", user.getIdentificationType());
        q.setParameter("number", user.getIdentificationNumber());

        try {
            q.getSingleResult();
            return true;
        } catch (NoResultException e) {
            return false;
        }//fin carch    

    }

    public boolean bFindByUserName(Users user, int op) {
        factory = Persistence.createEntityManagerFactory(PU);
        EntityManager em = factory.createEntityManager();
        Query q = null;

        if (op == 0) {
            //guardar
            q = em.createNamedQuery("Users.bFindUserName");
        } else {
            //modificar
            q = em.createNamedQuery("Users.bFindUserNameEdit");
            q.setParameter("id", user.getId());
        }//fin if

        q.setParameter("username", user.getUserName());

        try {
            q.getSingleResult();
            return true;
        } catch (NoResultException e) {
            return false;
        }//fin carch    

    }

    public List<Users> findLike(String cadena) throws Exception {
        factory = Persistence.createEntityManagerFactory(PU);
        EntityManager em = factory.createEntityManager();
        String sQuery = "SELECT o FROM Users o WHERE "
                + "TRIM(LOWER(o.surname)) LIKE '" + cadena.trim().toLowerCase() + "%'";
        Query q = em.createQuery(sQuery);
        return q.getResultList();
    }

    public List<Users> findAllButLoguer(Users user) throws Exception {
        factory = Persistence.createEntityManagerFactory(PU);
        EntityManager em = factory.createEntityManager();
        Query query = null;
        String q = "SELECT u FROM Users u WHERE u.id <>:id "
                + "AND u.userType <>:admin "
                + "AND u.userType <>:cuia";
        //      + "AND NOT EXISTS (SELECT us FROM Users us JOIN u.projectUserLst p WHERE p.project =:project)";

        query = em.createQuery(q);

        query.setParameter("id", user.getId());
        query.setParameter("admin", UserType.UNIVERSITY_ADMINISTRATOR);
        query.setParameter("cuia", UserType.CUIA);
        //query.setParameter("project", project);

        return query.getResultList();
    }

}
